/*
 * @Author: Ma Xiaohan
 * @Date: 2018-08-19 15:15:38
 * @Last Modified by: Xiaohan Ma
 * @Last Modified time: 2018-08-31 18:30:23
 */
const fs = require('fs');
const path = require('path');
const rename = think.promisify(fs.rename, fs);
const assert = require('assert');
const BasicController = require('../BasicController.js');

module.exports = class extends BasicController {
  constructor(ctx) {
    super(ctx);
    assert(think.isFunction(this.model), 'this.model must be a function');
    this.MODEL = this.ctx.state.ApiInfo.model;
    this.params = {
      model: this.MODEL,
      id: this.ID,
      data: this.post()
    };
  }

  //  上传项目图片或文件
  async uploadfilesAction() {
    try {
      const params = this.params;
      const file = this.file();
      const fileKeys = Object.getOwnPropertyNames(file);

      if (think.isEmpty(fileKeys)) {
        return this.fail('上传失败,图片不能为空');
      }
      for (const i of fileKeys) {
        let files = [];
        // 单张图片接收到的file 参数为object，多图片为array，统一将其转化为array
        if (think.isObject(file[i])) {
          files.push(file[i]);
        } else if (think.isArray(file[i])) {
          files = [...file[i]];
        }

        for (const item of files) {
          const fileid = think.uuid('v4');
          let name = path.extname(item.name);
          // name = `${think.md5(item.name)}${name}`;
          name = `${fileid}${name}`;
          const filepath = path.join(think.ROOT_PATH, `runtime/upload/projectFileInfo/${params.data.project_id}/${item.name}`);
          think.mkdir(path.dirname(filepath));
          await rename(item.path, filepath);
          const filedata = {};
          filedata.id = fileid;
          filedata.file_path = filepath;
          filedata.project_id = params.data.project_id;
          await this.model(params.model).uploadfilesAction(filedata);
          // 上传单张图片
          return this.success({id: fileid}, '上传成功');
        }
      }

      // const backdata = await this.model(params.model).getFilesListByIdAction(params.data.project_id);
      // return this.success(backdata, '上传成功');
    } catch (error) {
      return this.fail(error, '上传失败');
    }
  };

  async downloadAction() {
    const {
      filepath
    } = this.post();
    // const imgpath = path.join(think.ROOT_PATH, 'a.txt');
    return this.ctx.download(filepath);
    // return this.success(imgpath);
  }

  async deleteFileAction() {
    const params = this.params;
    const filesInfo = await this.model(params.model).deleteFileAction(params.data);
    return this.success(filesInfo);
  }

  async getUploadFileByIdAction() {
    const params = this.params;
    params.data.id = this.ID;
    const fileInfo = await this.model(params.model).getUploadFileByIdAction(params.data);
    return this.success(fileInfo, '查询成功');
  }
  // 前台展示接口
  async getlistAction() {
    const params = this.params;
    const list = await this.model(params.model).getlistAction(params);
    const display = list.data.filter(element => {
      let richtext = element.richtext;
      if (think.isEmpty(richtext)) {
        richtext = '';
      } else {
        richtext = richtext.richtext.toString('utf-8');
      }
      element.richtext = richtext;
      return element;
    });
    return this.success(display, '获取成功');
  }
  async getoneAction() {
    const params = this.params;
    const display = await this.model(params.model).getoneAction(params);
    let richtext = display.richtext;
    if (think.isEmpty(richtext)) {
      richtext = '';
    } else {
      richtext = richtext.richtext.toString('utf-8');
    }
    display.richtext = richtext;
    return this.success(display, '获取成功');
  }
};

/**
 * @api {POST} /project_info/project_info/uploadfiles /uploadfiles
 * @apiDescription 项目文件上传
 * @apiGroup project_info
 * @apiParam {String} project_id 项目id
 *
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *{
 *   "code": 1001,
 *   "msg": {
 *      "project_id": "project_id can not be blank"
 *   }
 *}
 *{
 *   "code": 0,
 *  "msg": "上传失败,文件不能为空"
 *}
 * @apiSuccessExample {json} 正确返回值:
 *    {
 *   "code": 0,
 *     "msg": [
 *      {
 *           "id": "028b88a7-f144-49f2-8ec9-c4bd603b7b29",
 *          "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/projectImgInfo/undefined/20180729-161755.png",
 *          "project_id": "555"
 *      },
 *      {
 *          "id": "348e5efa-1d56-4956-a196-d16822d709f9",
 *          "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/projectImgInfo/undefined/20180729-161755.png",
 *          "project_id": "555"
 *      }
 *  ],
 *  "data": "上传成功"
 * }
 */

/**
 * @api {post} /project_info/project_info/download /download
 * @apiDescription 下载文件
 * @apiGroup project_info
 * @apiParam {String} filepath 文件url
 *
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 * {
 *  "code": 1001,
 *  "msg": {
 *      "filepath": "filepath can not be blank"
 *  }
 * }
 * @apiSuccessExample {json} 正确返回值:
 * 文件
 */

/**
 * @api {delete} /project_info/project_info/deleteFile /deleteFile
 * @apiDescription 根据id删除项目文件
 * @apiGroup project_info
 *
 * @apiParam {String} id 项目文件ID
 *
 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 * id: dda33412-8e95-40f9-92f5-f01df8dd2d21,
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 * {
 * "code": 1001,
 * "msg": {
 *      "id": "id can not be blank"
 *  }
 * }
 * @apiSuccessExample {json} 正确返回值:
 * {
 *  "code": 0,
 *  "msg": "删除成功",
 *  "data": 0
 * }
 */

/**
 * @api {get} /project_info/project_info/get /get
 * @apiDescription 根据id查询单个项目信息
 * @apiGroup project_info
 * @apiParam {String} id  项目ID
 *
 *
 * @apiParamExample {string} 请求参数格式:
 *    ?id=8b3a12bd-6195-403b-9fcc-a6a053d6d073
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *{
 *   "code": 1001,
 *   "msg": {
 *      "id": "id can not be blank"
 *   }
 *}
 * @apiSuccessExample {json} 正确返回值:
 * {
 *  "code": 0,
 *  "msg": "",
 *  "data": {
 *      "id": "8b3a12bd-6195-403b-9fcc-a6a053d6d073",
 *      "project_name": "实验室官网",
 *      "richtext": "重写实验室官网",
 *      "project_technology": "reactjs,antd-pro",
 *      "project_participants": "移动互联网前端实验室",
 *      "project_service_object": null,
 *      "project_leaders": "xxb",
 *      "project_start_time": "2018-08-07",
 *      "project_end_time": null,
 *       "status":0,
 *      "imginfo": []
 *  }
 * }
 */

/**
 * @api {get} /project_info/project_info/getUploadFileById /getUploadFileById
 * @apiDescription 根据id查询单个项目文件信息
 * @apiGroup project_info
 * @apiParam {String} id  项目文件ID
 *
 *
 * @apiParamExample {string} 请求参数格式:
 *    ?id=8b3a12bd-6195-403b-9fcc-a6a053d6d073
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *{
 *   "code": 1001,
 *   "msg": {
 *      "id": "id can not be blank"
 *   }
 *}
 * @apiSuccessExample {json} 正确返回值:
 * {
    "code": 0,
    "msg": "查询成功",
    "data": {
        "id": "153b2e5e-e77f-4271-9295-b8a6acb7fc3b",
        "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/projectFileInfo/4f4edc98-1b9f-4a13-9a29-1e8f2480523a/2018-07-31 15-09-24 的屏幕截图.png",
        "project_id": "4f4edc98-1b9f-4a13-9a29-1e8f2480523a"
    }
}
 */

/**
 * @api {delete} /project_info/project_info/del /del
 * @apiDescription 根据id删除项目
 * @apiGroup project_info
 *
 * @apiParam {String} id 项目ID
 *
 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 * id: dda33412-8e95-40f9-92f5-f01df8dd2d21,
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 * {
 * "code": 1001,
 * "msg": {
 *      "id": "id can not be blank"
 *  }
 * }
 * @apiSuccessExample {json} 正确返回值:
 * {
 *  "code": 0,
 *  "msg": "",
 *  "data": 1
 * }
 */

/**
 * @api {post} /project_info/project_info/edit /edit
 * @apiDescription 根据id查询修改项目信息
 * @apiGroup project_info
 *
 * @apiParam {String} id 项目ID
 * @apiParam {String} richtext 富文本内容
 * @apiParam {String} [project_name] 项目名字
 * @apiParam {String} [project_technology] 项目所用技术
 * @apiParam {String} [project_participants] 项目参与者
 * @apiParam {String} [project_service_object] 项目服务对象
 * @apiParam {String} [project_leaders] 项目负责人
 * @apiParam {String} [project_start_time] 项目开始时间
 * @apiParam {String} [project_end_time] 项目截止时间
 * @apiParam {Number} [status] 状态

 *
 * @apiParamExample {object} 请求参数格式:
 *  {
 *  id: '18d92789-0240-4878-9b94-41078f2dbd50',
 *  project_name: 'project',
 *  richtext: '<p>你说啊这个项目是什么名字</p>',
 *  project_leaders: 'ddddd'
 * }
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 * {
 * "code": 1001,
 * "msg": {
 *      "id": "id can not be blank"
 *  }
 * }
 * @apiSuccessExample {json} 正确返回值:
 *{
 *   "errno": 0,
 *   "errmsg": "",
 *   "data": 1
*}
*/

/**
 * @api {POST} /project_info/project_info/listall /listall
 * @apiDescription 获取全部项目信息列表/根据条件获取项目信息列表
 * @apiGroup project_info
 *
 * @apiParam {String} [id] 项目ID
 * @apiParam {String} [project_name] 项目名字
 * @apiParam {String} [richtext] 项目内容
 * @apiParam {String} [project_technology] 项目所用技术
 * @apiParam {String} [project_participants] 项目参与者
 * @apiParam {String} [project_service_object] 项目服务对象
 * @apiParam {String} [project_leaders] 项目负责人
 * @apiParam {String} [project_start_time] 项目开始时间
 * @apiParam {String} [project_end_time] 项目截止时间
 * @apiParam {Number} [status] 状态
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": ,
 *        "msg": "获取失败",
 *        "error": {
 *        },
 *     }
 *
 * @apiSuccessExample {json} 正确返回值:
 * {
 *  "code": 0,
 *  "msg": "",
 *  "data": [
 *      {
 *          "id": "18d92789-0240-4878-9b94-41078f2dbd50",
 *          "project_name": "project",
 *          "richtext": "你说啊这个项目是什么名字",
 *          "project_technology": "reactjs,antd-pro",
 *          "project_participants": "移动互联网前端实验室",
 *          "project_service_object": null,
 *          "project_leaders": "ddddd",
 *          "project_start_time": "2018-08-09",
 *          "project_end_time": "2018-08-10"，
 *          "status":0
 *      }
 *  ]
 * }
 */

/**
 * @api {POST} /project_info/project_info/listpage /listpage
 * @apiDescription 获取全部项目列表/根据条件分页获取项目列表
 * @apiGroup project_info
 *
 * @apiParam {String} [id] 项目ID
 * @apiParam {String} [project_name] 项目名字
 * @apiParam {String} [richtext] 项目内容
 * @apiParam {String} [project_technology] 项目所用技术
 * @apiParam {String} [project_participants] 项目参与者
 * @apiParam {String} [project_service_object] 项目服务对象
 * @apiParam {String} [project_leaders] 项目负责人
 * @apiParam {String} [project_start_time] 项目开始时间
 * @apiParam {String} [project_end_time] 项目截止时间
 * @apiParam {Number} [status] 状态
 * @apiParam {Number} [page] 页数
 * @apiParam {Number} [pageSize] 数据条数
 *
 *
 *  * @apiParamExample {get} Request-Example:
                 {
                   page:1,
                   pageSize:10,
                   project_start_time:2018-08-09
                  }
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": ,
 *        "msg": "获取失败",
 *        "error": {
 *        },
 *     }
 *
 * @apiSuccessExample {json} 正确返回值:
{
    "code": 0,
    "msg": "",
    "data": {
        "pageSize": 10,
        "current": 1,
        "pages": 1,
        "total": 2,
        "data": [
            {
                "id": "18d92789-0240-4878-9b94-41078f2dbd50",
                "project_name": "project",
                "richtext": "你说啊这个项目是什么名字",
                "project_technology": "reactjs,antd-pro",
                "project_participants": "移动互联网前端实验室",
                "project_service_object": null,
                "project_leaders": "ddddd",
                "project_start_time": "2018-08-09",
                "project_end_time": "2018-08-10"，
                 "status":0

            },
            {
                "id": "79cdb20c-dd2b-4955-83be-f17dd66b80f0",
                "project_name": "实验室官网",
                "richtext": "重写实验室官网",
                "project_technology": "reactjs,antd-pro",
                "project_participants": "移动互联网前端实验室",
                "project_service_object": null,
                "project_leaders": "xxb",
                "project_start_time": "2018-08-09",
                "project_end_time": null，
                "status":0
            }
        ]
    }
}
 */

/**
 * @api {POST} /project_info/project_info/add /add
 * @apiDescription 新增项目
 * @apiGroup project_info
 *
 * @apiParam {String} project_name 项目名字
 * @apiParam {String} [project_technology] 项目所用技术
 * @apiParam {String} [project_participants] 项目参与者
 * @apiParam {String} [project_service_object] 项目服务对象
 * @apiParam {String} [project_leaders] 项目负责人
 * @apiParam {String} [project_start_time] 项目开始时间
 * @apiParam {String} [project_end_time] 项目截止时间
 * @apiParam {Number} [status] 状态
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 * {
 * "code": 1001,
 * "msg": {
 *      "project_name": "project_name can not be blank"
 *  }
 * }
 *
 * @apiSuccessExample {json} 正确返回值:
 * {
 *  "code": 0,
 *  "msg": "",
 *  "data": {
 *      "id": "ea6ccba8-f47d-4a0c-b54b-eef565f56829"
 *  }
 * }
 */

/**
 * @api {get} /project_info/project_info/getone /getone 前台获取单条项目信息数据
 * @apiDescription 根据id查询单个项目信息
 * @apiGroup project_info
 * @apiParam {String} id  项目ID
 *
 *
 * @apiParamExample {string} 请求参数格式:
 *    ?id=8b3a12bd-6195-403b-9fcc-a6a053d6d073
 *
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *{
 *   "code": 1001,
 *   "msg": {
 *      "id": "id can not be blank"
 *   }
 *}
 * @apiSuccessExample {json} 正确返回值:
 * {
 *  "code": 0,
 *  "msg": "",
 *  "data": {
 *      "id": "8b3a12bd-6195-403b-9fcc-a6a053d6d073",
 *      "project_name": "实验室官网",
 *      "project_technology": "reactjs,antd-pro",
 *      "project_participants": "移动互联网前端实验室",
 *      "project_service_object": null,
 *      "project_leaders": "xxb",
 *      "project_start_time": "2018-08-07",
 *      "project_end_time": null,
 *      "status":0,
 *      "richtext": "重写实验室官网",
 *      "project_files": []
 *  }
 * }
 */
/**
 * @api {POST} /project_info/project_info/getlist /getlist 前台获取项目信息数据列表
 * @apiDescription 获取全部项目列表/根据条件分页获取项目列表
 * @apiGroup project_info
 *
 * @apiParam {String} [id] 项目ID
 * @apiParam {String} [project_name] 项目名字
 * @apiParam {String} [richtext] 项目内容
 * @apiParam {String} [project_technology] 项目所用技术
 * @apiParam {String} [project_participants] 项目参与者
 * @apiParam {String} [project_service_object] 项目服务对象
 * @apiParam {String} [project_leaders] 项目负责人
 * @apiParam {String} [project_start_time] 项目开始时间
 * @apiParam {String} [project_end_time] 项目截止时间
 * @apiParam {Number} [status] 状态
 * @apiParam {Number} [page] 页数
 * @apiParam {Number} [pageSize] 数据条数
 *
 *
 *  * @apiParamExample {get} Request-Example:
                 {
                   page:1,
                   pageSize:10,
                   project_start_time:2018-08-09
                  }
 * @apiVersion 1.0.0
 * @apiErrorExample {json} 错误返回值:
 *     {
 *        "code": ,
 *        "msg": "获取失败",
 *        "error": {
 *        },
 *     }
 *
 * @apiSuccessExample {json} 正确返回值:
{
    "code": 0,
    "msg": "",
    "data": {
        "pageSize": 10,
        "current": 1,
        "pages": 1,
        "total": 2,
        "data": [
                 {
            "id": "72c5f734-5059-4b54-b773-6cdadf3a0bc7",
            "project_name": "321132321",
            "project_technology": null,
            "project_participants": null,
            "project_service_object": null,
            "project_leaders": null,
            "project_start_time": null,
            "project_end_time": null,
            "status": 0,
            "richtext": "",
            "create_time": "2018-08-24 09:22:44",
            "project_files": [
                {
                    "id": "4af05f38-5815-466a-97bd-67d39be955b5",
                    "file_path": "/var/www/html/mobile_internet_website_server/runtime/upload/projectFileInfo/72c5f734-5059-4b54-b773-6cdadf3a0bc7/Ubuntu 64 位-2018-07-19-23-59-01.png",
                    "project_id": "72c5f734-5059-4b54-b773-6cdadf3a0bc7"
                },
                {
                    "id": "6422ec9c-8337-4821-ab18-5e616d573805",
                    "file_path": "/var/www/html/mobile_internet_website_server/runtime/upload/projectFileInfo/72c5f734-5059-4b54-b773-6cdadf3a0bc7/Ubuntu 64 位-2018-07-19-23-59-01.png",
                    "project_id": "72c5f734-5059-4b54-b773-6cdadf3a0bc7"
                }
            ]
        },
        {
            "id": "4a092042-d4cd-4985-aadf-3dfb6a4d58ee",
            "project_name": "123",
            "project_technology": "1,2",
            "project_participants": "web",
            "project_service_object": "zxz",
            "project_leaders": "XXB",
            "project_start_time": "2018-08-23",
            "project_end_time": "2018-08-24",
            "status": 0,
            "richtext": "<p>gsdf</p>",
            "create_time": "2018-08-23 11:01:08",
            "project_files": [
                {
                    "id": "15c24d21-5676-4856-a472-f9e2ac5e3230",
                    "file_path": "/home/mdalang/projects/mobile_internet_website_server/runtime/upload/projectFileInfo/4a092042-d4cd-4985-aadf-3dfb6a4d58ee/屏幕快照 2018-08-23 上午10.13.47.png",
                    "project_id": "4a092042-d4cd-4985-aadf-3dfb6a4d58ee"
                },
                {
                    "id": "a5e3d26e-172d-41ae-95e9-704355030b21",
                    "file_path": "/var/www/html/mobile_internet_website_server/runtime/upload/projectFileInfo/4a092042-d4cd-4985-aadf-3dfb6a4d58ee/Ubuntu 64 位-2018-07-19-23-59-01.png",
                    "project_id": "4a092042-d4cd-4985-aadf-3dfb6a4d58ee"
                },
                {
                    "id": "ed1e6476-13de-42f3-979a-95d01691fecf",
                    "file_path": "/var/www/html/mobile_internet_website_server/runtime/upload/projectFileInfo/4a092042-d4cd-4985-aadf-3dfb6a4d58ee/Ubuntu 64 位-2018-07-19-23-59-01.png",
                    "project_id": "4a092042-d4cd-4985-aadf-3dfb6a4d58ee"
                }
            ]
        },
      ]
    }
}
 */
